//
//  FindViewController.m
//  四年
//
//  Created by 中软国际088 on 15/11/3.
//  Copyright © 2015年 小二. All rights reserved.
//

#import "FindViewController.h"

@interface FindViewController ()
@property    UITableView * tableView;
@property    NSMutableArray * array;
@property    UIView * headerView;
@property    UIImageView * imageView;


@end

@implementation FindViewController

- (id)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化数组,并设置值
    _array = [[NSMutableArray alloc]init];
    [self setTableViewArray];
    //navigation设置
    [self initNavigation];
    //初始化_tableView
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //对headerView设置值
    [self initHeaderView];
}

//navigation设置
- (void)initNavigation{
    _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"add@2x"]];
    //弹框设置
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:_imageView];
    self.navigationItem.rightBarButtonItem = rightItem;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(work:)];
    [_imageView addGestureRecognizer:tap];
}


//navigation右边按钮
- (void)work:(id)select{
    FindAddActivityCtr * faac = [[FindAddActivityCtr alloc]init];
    [self.navigationController pushViewController:faac animated:YES];
}
//_tableView头部
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 4, WIDTH, 20)];
    Group * group = _array[section];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 4, WIDTH, 20)];
    label.text = group.GroupName;
    label.textColor = [UIColor grayColor];
    [view addSubview:label];
    return view;
}
//头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 25;
}
//尾部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Group * group = _array[section];
    
    return group.FindArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FindTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[FindTableViewCell alloc]init];
        Group * group = _array[indexPath.section];
        Public * public = group.FindArray[indexPath.row];
        NSString * str = public.FindIntroduce;
        cell.label.text = public.PublicName;
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        cell.imView1.image = [UIImage imageNamed:str];
    }
    return cell;
}
//选择tableView事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //储存indexpath
    SingleValue * single = [SingleValue getSingle];
    single.indexPath = indexPath;
    single.arr = _array;
    
    //跳转
    if (indexPath.section ==0) {
        UseViewCtr * uvc = [[UseViewCtr alloc]init];
        [self.navigationController pushViewController:uvc animated:YES];
    }
    else if (indexPath.section ==1){
        OrganizationViewCtr * ovc = [[OrganizationViewCtr alloc]init];
        [self.navigationController pushViewController:ovc animated:YES];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _array.count;
}
//设置_tableView值,并存到数组里  ／／ 需要改
- (void)setTableViewArray{
    
    Public * p1_1 = [[Public alloc]initWithName:@"约美食" :1 :@"ayuemeishi@2x"];
    Public * p1_2 = [[Public alloc]initWithName:@"约水吧" :1 :@"ayueshuiba@2x"];
    Public * p1_3 = [[Public alloc]initWithName:@"约运动" :1 :@"ayueyundong@2x"];
    Public * p1_4 = [[Public alloc]initWithName:@"约电影" :1 :@"ayuedianying@2x"];
    
    Group * group1 = [[Group alloc]initWithGroupName:@"周边约人" FindArray:@[p1_1,p1_2,p1_3,p1_4] GroupNumber:1];
    [_array addObject:group1];
    Public * p2_1 = [[Public alloc]initWithName:@"创建社团" :2 :@"achuangjianshetuan@2x"];
    Public * p2_2 = [[Public alloc]initWithName:@"查看社团" :2 :@"achakan2@2x"];
    
    Group * group2 = [[Group alloc]initWithGroupName:@"爱好集群" FindArray:@[p2_1,p2_2] GroupNumber:2];
    [_array addObject:group2];
}
//tableHeaderView
- (void)initHeaderView{
    
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    imageView.image = [UIImage imageNamed:@"runing.jpg"];
    [_headerView addSubview:imageView];
    _tableView.tableHeaderView=_headerView;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [_tableView reloadData];
}

@end
